package com.qzq.weibo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.qzq.weibo.R;
import com.qzq.weibo.activity.PublishActivity;
import com.qzq.weibo.entity.User;
import com.qzq.weibo.utils.WeiCozzz;

public class HomeFragment extends Fragment implements OnClickListener {

	/** 缓存fragment，避免多次创建 */
	private View rootView;

	private TextView tv_screenName;
	private ImageView iv_add;
	private Button tab_friends;
	private Button tab_top;

	private int nor_color;
	private int pre_color;

	private User user;
	private static final String TAG = "HomeFragment";

	public static HomeFragment getInstance() {

		return new HomeFragment();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (rootView != null) {
			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null) {
				parent.removeView(rootView);
			}
			return rootView;
		} else {
			View view = initView(inflater);
			initMember();

			rootView = view;
			return view;
		}
	}

	private View initView(LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.fragment_home, null);

		tv_screenName = (TextView) view
				.findViewById(R.id.home_bar_tv_screen_name);
		iv_add = (ImageView) view.findViewById(R.id.home_bar_iv_add);

		tab_friends = (Button) view.findViewById(R.id.home_bar_tab_friends);
		tab_top = (Button) view.findViewById(R.id.home_bar_tab_top);

		return view;
	}

	private void initMember() {
		nor_color = getResources().getColor(R.color.white);
		pre_color = getResources().getColor(R.color.text_blue);
		user = WeiCozzz.getInstance().getUser();

		tv_screenName.setText(user.getScreen_name());

		iv_add.setOnClickListener(this);
		tab_friends.setOnClickListener(this);
		tab_top.setOnClickListener(this);

		/** 默认显示首页 */
		onClickTabHome();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.home_bar_iv_add:
			Intent intent = new Intent(getActivity(), PublishActivity.class);
			getActivity().startActivity(intent);
			break;

		case R.id.home_bar_tab_friends:

			onClickTabFriend();

			break;

		case R.id.home_bar_tab_top:

			onClickTabHome();

			break;

		default:
			break;
		}
	}

	/** tab_friends的点击事件 */
	private void onClickTabFriend() {

		tab_friends.setSelected(true);
		tab_top.setSelected(false);

		tab_friends.setTextColor(pre_color);
		tab_top.setTextColor(nor_color);

		FragmentTransaction ft = getChildFragmentManager().beginTransaction();
		ft.replace(R.id.home_content, HomeTabFriendsFragment.getInstance());
		ft.commit();

	}

	/** tab_home的点击事件 */
	private void onClickTabHome() {

		tab_top.setSelected(true);
		tab_friends.setSelected(false);

		tab_friends.setTextColor(nor_color);
		tab_top.setTextColor(pre_color);

		FragmentTransaction ft = getChildFragmentManager().beginTransaction();
		ft.replace(R.id.home_content, HomeTabTopFragment.getInstance());
		ft.commit();

	}

}
